package com.travelbid.admin.service;

import java.util.List;
import org.hibernate.Query;

import com.travelbid.admin.dao.TNavigateDAO;
import com.travelbid.admin.dao.TProjetDAO;
import com.travelbid.admin.pojo.TNavigate;
import com.travelbid.admin.pojo.TProjet;
import com.travelbid.common.HibernateSessionFactory;
import com.travelbid.common.Paging;
import com.travelbid.common.PagingTool;

public class NavigateService {

	private TNavigateDAO navigatedao;
	private TProjetDAO projetdao;

	public void save(TNavigate tn) {
		navigatedao.save(tn);
	}

	public void delete(TNavigate tn) {
		navigatedao.delete(tn);
	}

	public TNavigate findById(Integer id) {
		return navigatedao.findById(id);
	}

	public void update(TNavigate tn) {
		navigatedao.save(tn);
	}

	
	@SuppressWarnings("unchecked")
	public List findAll() {
		// tyDAO.findAll();
		String hql = "from TProjet";
		List<TProjet> list = projetdao.getHibernateTemplate().find(hql);
		return list;
	}

	public void saveprojet(TProjet tr) {
		projetdao.save(tr);
	}

	public void deleteprojet(TProjet tr) {
		projetdao.delete(tr);
	}

	public TProjet findByProjetId(Integer id) {
		return projetdao.findById(id);
	}

	public void updateprojet(TProjet tr) {
		projetdao.save(tr);
	}

	
	public TNavigateDAO getNavigatedao() {
		return navigatedao;
	}

	public void setNavigatedao(TNavigateDAO navigatedao) {
		this.navigatedao = navigatedao;
	}

	public TProjetDAO getProjetdao() {
		return projetdao;
	}

	public void setProjetdao(TProjetDAO projetdao) {
		this.projetdao = projetdao;
	}

	// private TNavigateDAO navigateDAO = new TNavigateDAO();
	//	
	// public void save(TNavigate na){
	// Session session = (Session)
	// com.travelbid.common.HibernateSessionFactory.getSession();
	// // Session session = navigateDAO.getSession();
	// Transaction ts = session.beginTransaction();
	// try{
	// navigateDAO.save(na);
	// ts.commit();
	// }catch(Exception e){
	// ts.rollback();
	// e.printStackTrace();
	// }finally{
	// session.close();
	// }
	// }
	//	
	// public void delete(String[] id){
	// Session session = (Session)
	// com.travelbid.common.HibernateSessionFactory.getSession();
	// // Session session = navigateDAO.getSession();
	// Transaction ts = session.beginTransaction();
	// try{
	// for(int i=0;i<id.length;i++){
	// navigateDAO.delete(navigateDAO.findById(new Integer(id[i])));
	// }
	// ts.commit();
	// }catch(Exception e){
	// ts.rollback();
	// e.printStackTrace();
	// }finally{
	// session.close();
	// }
	// }
	//	
	@SuppressWarnings("unchecked")
	public List find(String keyword, Integer type, Paging p) {
		try {
			String queryString = "from TNavigate as na order by na.type,na.location";
			if (new Integer(-1).equals(type)) {
				if (keyword == null || "".equals(keyword.trim())) {
					queryString = "from TNavigate as na order by na.type,na.location";
				} else {
					queryString = "from TNavigate as na where (na.name like '%"
							+ keyword + "%' or na.url like '%" + keyword
							+ "%') order by na.type,na.location";
				}
			} else {
				if (keyword == null || "".equals(keyword.trim())) {
					queryString = "from TNavigate as na where na.type = "
							+ type + " order by na.location";
				} else {
					queryString = "from TNavigate as na where (na.name like '%"
							+ keyword + "%' or na.url like '%" + keyword
							+ "%') and na.type = " + type
							+ " order by na.location";
				}
			}
			Query queryObject = HibernateSessionFactory.getSession()
					.createQuery(queryString);
			// queryObject.setParameter(0, "%"+KeyWord+"%");
			if (p != null) {
				p.setTotalCount(queryObject.list().size());
				return PagingTool.getQueryResByPage(queryObject, p);
			}
			return queryObject.list();
		} catch (RuntimeException re) {
			throw re;
		} finally {
			HibernateSessionFactory.closeSession();
		}
	}
}
